<?php

namespace api\store;


class StoreLocal
{
    private static $store_name = 'store';

    private static $_store_path = null;


    public function __construct($store_name)
    {
        self::$_store_path = RUNTIME_PATH . self::$store_name . '/';
        if (!file_exists(self::$_store_path)) mkdir(self::$_store_path);
    }

    public static function data_get($name, $default = false)
    {
        $filename = self::$_store_path . $name;
        if (!is_file($filename))
            return $default;

        $content = file_get_contents($filename);
        if (false !== $content) {
            $content = unserialize($content);
            return $content;
        } else {
            return $default;
        }
    }

    public static function data_set($name, $value)
    {
        $filename = self::$_store_path . $name;
        $data = serialize($value);
        $result = file_put_contents($filename, $data);
        if ($result) {
            clearstatcache();
            return true;
        } else {
            return false;
        }
    }

}